﻿using Bestcode.MathParser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;

namespace Tubes4_Metnum.Machine
{
    public class Simpson3per8
    {
        public MathParser parser;
        public double [] dataX;
        public double [] dataY;
        public double a;
        public double b;
        public double h;
        public int n;
        public double result;

        public Simpson3per8(){
            parser = new MathParser();
        }

        public void setExpression(String exp)
        {
            parser.Expression = exp;
        }

        public double integral_data(double[] dX, double[] dY)
        {
            this.result = 0;
            this.dataX = dX; this.dataY = dY;
            this.n = dataX.Length - 1;
            this.a = dataX[0]; this.b = dataX[n];
            this.h = (b - a) / (n);

            double sigma = 0; 
            for (int k = 1; 3 * k <= n; k++)
            {
                for (int r = 1; r <= 4; r++)
                {
                    if ((r == 1) || (r == 4))
                    {
                        sigma += this.dataY[3 * k - 4 + r];
                    }
                    else
                    {
                        sigma += 3 * this.dataY[3 * k - 4 + r];
                    }
                }
            }
            result = (result + sigma) * h * 3 / 8;

            return result;
        }

        public double integral(String exp, double a, double b, int n)     // n harus genap
        {
            //n HARUS kelipatan 3
            //a titik mulai, b titik akhir, n jumlah pembagi, I hasil
            result = 0;
            parser.Expression = exp;
            this.a = a; this.b = b; this.n = n;
            this.h = (b - a) / (n);
            this.dataX = new double[n + 1];
            this.dataY = new double[n + 1];

            double sigma = 0;
            double x = a;
            for (int k = 1; 3 * k <= n; k++)
            {
                for (int r = 1; r <= 4; r++)
                {
                    x = a + (3 * k - 4 + r) * h;
                    this.dataX[3 * k - 4 + r] = x;
                    if ((r == 1) || (r == 4))
                    {
                        parser.X = x;
                        sigma += (double)parser.Value;

                    }
                    else
                    {
                        parser.X = x;
                        sigma += 3 * (double)parser.Value;
                    }
                    this.dataY[3 * k - 4 + r] = (double)parser.Value;
                }
            }
            result = (result + sigma) * h * 3 / 8;
            return result;
        }

        // ubah 
        public void saveFile(string filePath)
        {
            Document document = new Document();
            try
            {
                string ext = filePath.Substring(filePath.Length - 4, 4);
                if (ext.CompareTo(".pdf") != 0) filePath += ".pdf";
                PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
                document.Open();

                /* untuk Trapesium */
                document.Add(new Paragraph("Metode Simpson 3/8"));

                PdfPTable ptable;
                PdfPCell pcell;
                document.Add(new Paragraph("a = " + this.a + "; b = " + this.b + "; h = " + this.h + "; n = " + this.n));
                ptable = new PdfPTable(2);
                iTextSharp.text.Font arial7 = FontFactory.GetFont("ARIAL", 7);
                pcell = new PdfPCell(new Phrase(new Chunk("X", arial7)));
                ptable.AddCell(pcell);
                pcell = new PdfPCell(new Phrase(new Chunk("Y", arial7)));
                ptable.AddCell(pcell);
                for (int rows = 0; rows < this.dataX.Length; rows++)
                {
                    pcell = new PdfPCell(new Phrase(new Chunk(this.dataX[rows].ToString(), arial7)));
                    ptable.AddCell(pcell);
                    pcell = new PdfPCell(new Phrase(new Chunk(this.dataY[rows].ToString(), arial7)));
                    ptable.AddCell(pcell);
                }
                document.Add(new Paragraph("\n"));
                document.Add(ptable);
                document.Add(new Paragraph("Hasil integral: " + this.result));
            }
            catch (DocumentException de)
            {

            }
            catch (IOException ioe)
            {

            }
            document.Close();
        }

    }
}
